package com.rongshu.view.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.discovery.bean.AuthorDetailBean;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class AuthorListAtAdapter extends RecyclerView.Adapter<AuthorListAtAdapter.ViewHolder> {

    private Context context;
    private List<AuthorDetailBean> authorDetailBeans;

    //interface
    private OnItemClickListener mOnItemClickListener = null;

    public AuthorListAtAdapter(Context context) {
        this.context = context;
    }

    public void setAuthorDetailBeans(List<AuthorDetailBean> list) {
        this.authorDetailBeans = list;
    }

    public List<AuthorDetailBean> getAuthorDetailBeans() {
        return this.authorDetailBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_author_at_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        AuthorDetailBean bean = authorDetailBeans.get(i);
        GlideUtils.loadImageView(context, viewHolder.logo, bean.getAvatar());
        viewHolder.name.setText(authorDetailBeans.get(i).getBi_name());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return authorDetailBeans == null ? 0 : authorDetailBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private ImageView logo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //interface
    public interface OnItemClickListener {

        void onItemClick(int position);
    }
}
